package userInterface;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JTable;

import model.Auction;
import model.Control;
import javax.swing.JLabel;
import javax.swing.JButton;

/**
 * The Auction List GUI - displays a list of upcoming auctions.
 * @author Igor
 * minor modifications by Daniel Bartholomew
 */

public class AuctionListView extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JDesktopPane jDesktopPane = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private ItemsAuctionTableModel autionsTableModel;
	private JLabel lblHeader = null;
	private JButton btnBack = null;
	
	private Control my_control;

	/**
	 * This method initializes jDesktopPane
	 *
	 * @return javax.swing.JDesktopPane
	 */
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			lblHeader = new JLabel();
			lblHeader.setBounds(new Rectangle(236, 8, 146, 23));
			lblHeader.setText("Current Auctions");
			jDesktopPane = new JDesktopPane();
			jDesktopPane.add(getJScrollPane(), null);
			jDesktopPane.add(lblHeader, null);
			jDesktopPane.add(getBtnBack(), null);
		}
		return jDesktopPane;
	}

	/**
	 * This method initializes jScrollPane
	 *
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(28, 43, 576, 349));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable
	 *
	 * @return javax.swing.JTable
	 */
	private JTable getJTable() {
		if (jTable == null) {
			autionsTableModel = new ItemsAuctionTableModel();
			jTable = new JTable(autionsTableModel);
			jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			jTable.getColumnModel().getColumn(0).setPreferredWidth(573);
			// For use with mouse listener and events on table when clicked
	        jTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
	        jTable.getTableHeader().setReorderingAllowed(false);
	        jTable.addMouseListener(new java.awt.event.MouseAdapter() {

	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                tableMouseClicked();
	            }
	        });
		}
		return jTable;
	}
	private void tableMouseClicked() {
        int index = jTable.getSelectedRow();
        if (index != -1) {
        	AuctionView auctionView = new AuctionView(autionsTableModel.getAuction(index), my_control);
        	auctionView.setLocationRelativeTo(null);
        	auctionView.setVisible(true);
        	dispose();
        }
    }
	/**
	 * This method initializes btnBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton("Back");
			btnBack.setBounds(new Rectangle(495, 404, 106, 31));
			btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					BidderMenuView bmv = new BidderMenuView(my_control);
					
					bmv.setLocationRelativeTo(null);
					bmv.setVisible(true);
					dispose();
				}
			});
		}
		return btnBack;
	}

	/**
	 * This is the default constructor
	 */
	public AuctionListView(Control control) {
		super();
		initialize();
		my_control = control;
		ArrayList<Auction> auctions = my_control.getAuctions();
		for (Auction auction: auctions)
		{
			autionsTableModel.add(auction);
		}
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setSize(640, 480);
		this.setContentPane(getJContentPane());
		this.setTitle("Auction List View");
		this.setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

}
